Hey All,
I am trying to make a graph with colored bars that shade in specific ranges of years which are on the x axis. The Y axis should go to 100 and all the bars should be the same height. I am getting the error too few variables specified. Any suggestions? Here is the code: clear
cd "S:\Julianna M\policybars"
* Sample data generation
input str30 type_category year start_year end_year
"recession" 2001 2001 2002
"recession" 2007 2007 2009
"recession" 2020 2020 2021
"medicaid_expansion" 2014 2014 2030
"csr_reduction" 2017 2017 2018
"silver_loading" 2018 2018 2026
end
* Convert to numeric values
gen start_date = mdy(1, 1, start_year)
gen end_date = mdy(12, 31, end_year)
* Create a unique numeric identifier for each time period within each type_category
egen time_variable = group(type_category year)
* Create a binary variable indicating the start and end of each event
gen event_start = 1 if time_variable == start_date
gen event_end = -1 if time_variable == end_date
* Calculate cumulative sum of the events
gen cum_sum = sum(event_start + event_end)
* Generate bar chart
twoway bar cum_sum if inrange(year, 2001, 2002), barwidth(2) barcolor(blue) ///
|| ///
bar cum_sum if inrange(year, 2007, 2009), barwidth(2) barcolor(green) ///
|| ///
bar cum_sum if inrange(year, 2020, 2021), barwidth(2) barcolor(red) ///
|| ///
bar cum_sum if inrange(year, 2014, 2030), barwidth(2) barcolor(yellow) ///
|| ///
bar cum_sum if inrange(year, 2017, 2018), barwidth(2) barcolor(orange) ///
|| ///
bar cum_sum if inrange(year, 2018, 2026), barwidth(2) barcolor(purple) ///
, ///
title("Policy Events Bar Chart") ///
ylabel(0(20)120) ///
ytitle("Number Employees") ///
xtitle("Years (Increasing by 2)") ///
xlab(2001(2)2026) ///
legend(order(1 "2001-2002" 2 "2007-2009" 3 "2020-2021" 4 "2014-2030" 5 "2017-2018" 6 "2018-2026")) ///
I am trying to make a graph with colored bars that shade in specific ranges of years which are on the x axis. The Y axis should go to 100 and all the bars should be the same height. I am getting the error too few variables specified. Any suggestions? Here is the code: clear
cd "S:\Julianna M\policybars"
* Sample data generation
input str30 type_category year start_year end_year
"recession" 2001 2001 2002
"recession" 2007 2007 2009
"recession" 2020 2020 2021
"medicaid_expansion" 2014 2014 2030
"csr_reduction" 2017 2017 2018
"silver_loading" 2018 2018 2026
end
* Convert to numeric values
gen start_date = mdy(1, 1, start_year)
gen end_date = mdy(12, 31, end_year)
* Create a unique numeric identifier for each time period within each type_category
egen time_variable = group(type_category year)
* Create a binary variable indicating the start and end of each event
gen event_start = 1 if time_variable == start_date
gen event_end = -1 if time_variable == end_date
* Calculate cumulative sum of the events
gen cum_sum = sum(event_start + event_end)
* Generate bar chart
twoway bar cum_sum if inrange(year, 2001, 2002), barwidth(2) barcolor(blue) ///
|| ///
bar cum_sum if inrange(year, 2007, 2009), barwidth(2) barcolor(green) ///
|| ///
bar cum_sum if inrange(year, 2020, 2021), barwidth(2) barcolor(red) ///
|| ///
bar cum_sum if inrange(year, 2014, 2030), barwidth(2) barcolor(yellow) ///
|| ///
bar cum_sum if inrange(year, 2017, 2018), barwidth(2) barcolor(orange) ///
|| ///
bar cum_sum if inrange(year, 2018, 2026), barwidth(2) barcolor(purple) ///
, ///
title("Policy Events Bar Chart") ///
ylabel(0(20)120) ///
ytitle("Number Employees") ///
xtitle("Years (Increasing by 2)") ///
xlab(2001(2)2026) ///
legend(order(1 "2001-2002" 2 "2007-2009" 3 "2020-2021" 4 "2014-2030" 5 "2017-2018" 6 "2018-2026")) ///
Comment